package tongwentongshu.com.app.presenter;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v4.util.ArrayMap;
import android.text.TextUtils;
import android.util.Log;

import java.util.Map;

import tongwentongshu.com.app.bean.AppItem;
import tongwentongshu.com.app.bean.PersonalBean;
import tongwentongshu.com.app.bean.SignBean;
import tongwentongshu.com.app.contract.PersonalCenterContract;
import tongwentongshu.com.app.db.Cache;
import tongwentongshu.com.app.network.ResponseSubscriber;
import tongwentongshu.com.app.update.UpdateFractory;

/**
 * Created by XiongBin on 2017/7/10.
 */

public class PersonalCenterPresenter implements PersonalCenterContract.Presenter {
    private PersonalCenterContract.View personalCenterView;
    private Map<String, String> map;

    public PersonalCenterPresenter(PersonalCenterContract.View personalCenterView) {
        this.personalCenterView = personalCenterView;
    }

    @Override
    public void initMyInfo() {
        map = new ArrayMap<>();
        if (!TextUtils.isEmpty(Cache.getToken()))
            map.put("token", Cache.getToken());
        Log.e("token", Cache.getToken() + "");
        UpdateFractory.getBuild()
                .name("PersonalShowCall")
                .map(map)
                .build()
                .execute(new ResponseSubscriber<PersonalBean>() {

                    @Override
                    public void onFailure(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onSuccess(PersonalBean per, String msg) {
                        personalCenterView.onSuccess(per);
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }
                });

    }

    @Override
    public void initSign() {
        map = new ArrayMap<>();
        map.put("token", Cache.getToken());
        UpdateFractory.getBuild()
                .name("SigninCall")
                .map(map)
                .build()
                .execute(new ResponseSubscriber<SignBean>() {

                    @Override
                    public void onFailure(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onSuccess(SignBean per, String msg) {
                        personalCenterView.onSignSuccess(per);
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }
                });

    }

    @Override
    public void updataHead(String url) {
        map = new ArrayMap<>();
        map.put("token", Cache.getToken());
        map.put("imagehead", url);
        UpdateFractory.getBuild()
                .name("UseravatarCall")
                .map(map)
                .build()
                .execute(new ResponseSubscriber<AppItem>() {

                    @Override
                    public void onFailure(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onSuccess(AppItem per, String msg) {
                        personalCenterView.onHeadSuccess(msg);
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }
                });

    }


}
